﻿using System;
using System.Data;
using System.Collections.Generic;
using SERP.DAO;
namespace SERP.Controllers{
public class UserRolesController : UserRolesControllerBase{
	public UserRolesController()
		: base()
	{
	}	
	private DataTable _Table;
	private UserRolesEntity oUserRoles;	
	public UserRolesEntity UserRolesObject{
		get{
			return oUserRoles;
		}
		set{
			oUserRoles=value;
		}
	}
	public DataTable Table{
		get{
			if(_Table==null&&AllowCaching){				
				_Table=Cache.Get<DataTable>(CACHE_KEY_DS);
			}
			return _Table;
		}
		set{
			_Table=value;
		}
	}
	public void LoadAllDSUserRoles(bool clearCache){
		bool bForce=clearCache;
		if(bForce||_Table==null)
		{
			DataSet ds=Service.SelectAllDS();
			_Table=ds.Tables[0];
			AddIndexToTable(_Table);
			if(AllowCaching)
				AddCache(CACHE_KEY_DS,_Table);			
		}				
	}	
	public void AddIndexToTable(){
		AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		ResetTableIndex(_Table);
	}
	public void LoadObject(Int32 UserRoleId){
		UserRolesEntity oLoad=FindUserRoles(UserRoleId);
		if(oLoad==null){
			LoadAllUserRoles(true);
			oUserRoles=FindUserRoles(UserRoleId);
		}
		else{
			oUserRoles=oLoad;
		}
	}
	public bool AddNew(){
		if(oUserRoles==null)return false;
		bool result = Service.Save(oUserRoles);
		if(result){
			AddObjectToCache(oUserRoles);
		}
		return result;
	}
	public bool Update(){
		if(oUserRoles==null)return false;
		return Service.Update(oUserRoles);	
	}
	public bool Delete(){
		if(oUserRoles==null)return false;		
		bool retvalue = Service.Delete(oUserRoles);
		if(retvalue){			
			RemoveObjectFromCache(oUserRoles);
		}
		return retvalue;
	}	
	public bool DeleteByID(Int32 UserRoleId){
		bool retvalue = Service.DeleteByID(UserRoleId);		
		if(retvalue){
			UserRolesEntity oDeleted=FindUserRoles(UserRoleId);
			RemoveObjectFromCache(oDeleted);
		}
		return retvalue;
	}	
	public void ResetObject(){
		this.oUserRoles = new UserRolesEntity();
	}
	public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.UserRolesObject,row);
    }
    public UserRolesEntity LoadUserRoles(Int32 UserRoleId){			
		UserRolesEntity o=Service.SelectOne(UserRoleId);		
		return o;
	}
	#region Cached
    private static List<UserRolesEntity>_ListUserRoles;
	private static bool _AllowCaching=true;
	public static bool AllowCaching{
		get{return _AllowCaching;}
		set{AllowCaching=value;}
	}	
	public static void ClearCache(){
			if(!_AllowCaching)return;
			Cache.Remove(CACHE_KEY_DS);
			Cache.Remove(CACHE_KEY_LS);
	}
	private const string CACHE_KEY_DS="UserRolesDS";
	private const string CACHE_KEY_LS="UserRolesLS";
	private void AddObjectToCache(UserRolesEntity newObject){
		if(!AllowCaching)return;
		GetCacheList().Add(newObject);
	}
	private void RemoveObjectFromCache(UserRolesEntity oRemove)
	{
		if(!AllowCaching)return;
		if(oRemove!=null)
		GetCacheList().Remove(oRemove);
	}
	private static List<UserRolesEntity>GetCacheList(){
		if(Cache==null)return default(List<UserRolesEntity>);
		List<UserRolesEntity>ls= Cache.Get<List<UserRolesEntity>>(CACHE_KEY_LS);
		if(ls==null){
			ls = new List<UserRolesEntity>();
			AddCache(CACHE_KEY_LS,ls);
		}
		return ls;
	}
	public List<UserRolesEntity>ListUserRoles{
		get{
			return LoadAllUserRoles(false);
		}
	}
	public static List<UserRolesEntity>ListUserRolesStatic{
		get{
			LoadAllUserRoles(false);
			return _ListUserRoles;
		}
	}
	public UserRolesEntity FindUserRoles(Int32 UserRoleId){
		if(AllowCaching){
			_ListUserRoles=GetCacheList();
		}		
		UserRolesEntity oResult=_ListUserRoles.Find(delegate(UserRolesEntity oFind){
			bool result=true; 
			result&=oFind.UserRoleId.Equals(UserRoleId);
			return result;
		 });
		return oResult;
	}
	public static List<UserRolesEntity>LoadAllUserRoles(bool clearCache){
		_ListUserRoles = new List<UserRolesEntity>();
		if(AllowCaching){
			_ListUserRoles=GetCacheList();
		}
		if(_ListUserRoles==null||_ListUserRoles.Count==0)
			clearCache=true;
		if(!clearCache)return _ListUserRoles;
		_ListUserRoles=new List<UserRolesEntity>(Service.SelectAll());		
		if(AllowCaching)
			AddCache(CACHE_KEY_LS,_ListUserRoles);
		return _ListUserRoles;
	}
	#endregion
}
}
